﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Related.aspx.cs" Inherits="NewsAggregator.Related" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Header" runat="server">
     <div data-role="header" data-theme="f">
		<h1>Related Stories</h1>
    </div><!-- /header -->
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    
     <script type="text/javascript" language="javascript">
         $(document).bind('pageinit', function () {
             //$("#liHome").addClass("ui-btn-active");
             //$("#liHome").addClass("ui-state-persist");
             //$("#liCategory").removeClass("ui-btn-active");
             //$("#liSearch").removeClass("ui-btn-active");
         });
         var PageNum = 0;
         function NextPage() {
             Block();
             PageNum++;
             var pageUrl = '<%=ResolveUrl("~/WebService/DataService.asmx")%>';
            $.ajax({
                type: "POST",
                url: pageUrl + "/GetRelatedStories",
                data: "{'storyID':'" + $('#<%= hdnStoryID.ClientID %>').val() + "','pageNumber':'" + PageNum + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (results) {
                    UnBlock();
                    $("#listStories").empty();
                    var items = [];
                    for (var key in results.d) {
                        items.push('<li><a onclick="UpdateHits(' + results.d[key].Id + ')" href="' + results.d[key].Link + '">' + results.d[key].Title + '</a><span class="ui-li-count">' + results.d[key].Hits + '</span><a href="javascript:Related(' + results.d[key].Id + ')">Related Stories</a></li>');
                    }

                    if (PageNum == 0) {
                        $('#listStories').append(items.join(''));
                        $('#listStories').append('<li><a href="javascript:NextPage();">Next 10</a></li>');
                    } else {

                        $('#listStories').append('<li><a href="javascript:PrevPage();">Previous 10</a></li>');
                        $('#listStories').append(items.join(''));
                        $('#listStories').append('<li><a href="javascript:NextPage();">Next 10</a></li>');
                    }
                    $('#listStories').listview('refresh');
                }
            });
        }
        function PrevPage() {
            Block();
            PageNum--;
            var pageUrl = '<%=ResolveUrl("~/WebService/DataService.asmx")%>';
             $.ajax({
                 type: "POST",
                 url: pageUrl + "/GetRelatedStories",
                 data: "{'storyID':'" + $('#<%= hdnStoryID.ClientID %>').val() + "','pageNumber':'" + PageNum + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (results) {
                    UnBlock();
                    $("#listStories").empty();
                    var items = [];
                    for (var key in results.d) {
                        items.push('<li><a onclick="UpdateHits(' + results.d[key].Id + ')" href="' + results.d[key].Link + '">' + results.d[key].Title + '</a><span class="ui-li-count">' + results.d[key].Hits + '</span><a href="javascript:Related(' + results.d[key].Id + ')">Related Stories</a></li>');
                    }

                    if (PageNum == 0) {
                        $('#listStories').append(items.join(''));
                        $('#listStories').append('<li><a href="javascript:NextPage();">Next 10</a></li>');
                    } else {

                        $('#listStories').append('<li><a href="javascript:PrevPage();">Previous 10</a></li>');
                        $('#listStories').append(items.join(''));
                        $('#listStories').append('<li><a href="javascript:NextPage();">Next 10</a></li>');
                    }
                    $('#listStories').listview('refresh');
                }
            });
        }

         function UpdateHits(newsID) {
             var pageUrl = '<%=ResolveUrl("~/WebService/RecordHitsWS.asmx")%>'
            $.ajax({
                type: "POST",
                url: pageUrl + "/UpdateHits",
                data: "{'newsid':'" + newsID + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccessCall,
                error: OnErrorCall
            });
        }

        function OnSuccessCall(response) {

        }

        function OnErrorCall(response) {
            //alert("Error changing the status");
        }
        
        function Related(storyID) {
            window.location = "Related.aspx?story=" + storyID;
        }

    </script>

    <div data-role="content">
        <asp:HiddenField runat="server" ID="hdnStoryID"/>
	  <ul id="listStories" data-role="listview" data-inset="true">
            <asp:Repeater ID="topStroriesRepeater" runat="server">
                    <itemtemplate>
                        <li>
                            <a onclick="UpdateHits('<%# DataBinder.Eval(Container.DataItem, "ID") %>')" href="<%# DataBinder.Eval(Container.DataItem, "Link") %>"><%# DataBinder.Eval(Container.DataItem, "Title") %></a>
                            <span class="ui-li-count"><%# DataBinder.Eval(Container.DataItem, "Hits") %></span>
                           <a href="javascript:Related('<%# DataBinder.Eval(Container.DataItem, "ID") %>')">Related Stories</a>
                        </li>                           
                    </itemtemplate>
                <FooterTemplate>
                    <li><a href="javascript:NextPage();">Next 10</a></li>
                </FooterTemplate>
            </asp:Repeater>
		</ul>
	</div>
</asp:Content>
